<?php

namespace App\Http\TraitClass;

trait Response
{
    /**
     * 一般返回格式
     * @param null $data
     * @param int $error_code
     * @param string $error_msg
     * @return array
     */
    protected function return($data = null, $error_code = 0, $error_msg = '')
    {
        return [
            'data' => $data,
            'error_code' => $error_code,
            'error_msg' => $error_msg
        ];
    }

    /**
     * 分页数据对外返回格式
     * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginate
     * @return array
     */
    protected function returnPage(\Illuminate\Contracts\Pagination\LengthAwarePaginator $paginate)
    {
        return [
            'data' => $this->pageArray($paginate),
            'error_code' => 0,
            'error_msg' => ''
        ];
    }

    /**
     * 分页数据
     * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginate
     * @return array
     */
    private function pageArray(\Illuminate\Contracts\Pagination\LengthAwarePaginator $paginate)
    {
        return [
            'items' => $paginate->items(),
            'total' => $paginate->total(),
            'per_page' => $paginate->perPage(),
            'last_page' => $paginate->lastPage(),
            'current_page' => $paginate->currentPage(),
        ];
    }
}
